package com.flycms.module.qasystem.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.flycms.constant.StatusConst;
import com.flycms.module.comment.dao.QuestionsCountDao;
import com.flycms.module.qasystem.dao.AnswersDao;
import com.flycms.module.qasystem.dao.QuestionsDao;
import com.flycms.module.qasystem.model.Answers;
import com.flycms.module.qasystem.model.Questions;

/**
 * 问题类Service
 *
 * <br/>Copyright 2020
 * <br/>All rights reserved
 * @author 沁言訫語
 * @version 2020年2月17日
 * @Date 2020年2月17日
 */

@Service
public class QuestionsService {

    @Autowired
    protected QuestionsDao questionsDao;
    
    @Autowired
    protected AnswersDao answersDao;
    
    @Autowired
    protected QuestionsCountDao questionCountDao;
    
    @Transactional
    public int addQuestion(Questions question) throws Exception {
        return questionsDao.addQuestion(question);
    }
    
    @Transactional
    public int editQuestion(Long id, Integer status,
            String opinion, Date updateTime, Long updateUserId) throws Exception {
        Questions question = new Questions();
        question.setId(id);
        question.setStatus(status);
        question.setOpinion(opinion);
        question.setUpdateTime(updateTime);
        return questionsDao.editQuestion(question);
    }
    
    @Transactional
    public int increaseQuestionBrowsedNum(Long id, Integer increaseNum) throws Exception {
        return questionsDao.increaseQuestionBrowsedNum(id, increaseNum);
    }
    
    public List<Questions> getQuestionList(Long proposeUserId, Integer status, String title, String orderby,
            String order, int pageIndex, int pageSize) throws Exception {
        // 用户访问只看到开启中的问题列表
        orderby = "create_time";
        order = "desc";
        List<Questions> questList = questionsDao.getQuestionList(proposeUserId, title, status, orderby, order,
                pageIndex, pageSize);
        return questList;
    }
    
    public int getQuestionCount(Long proposeUserId, String title, Integer status) throws Exception {
        return questionsDao.getQuestionCount(proposeUserId, title, status);
    }
    
    public List<Questions> getQuestionListForCustom(Long proposeUserId, String title, int pageIndex, int pageSize)
            throws Exception {
        // 用户访问只看到开启中的问题列表
        List<Questions> questList = questionsDao.getQuestionListForCustom(title, StatusConst.OPEN.value(),
                pageIndex, pageSize);
        //
        List<Long> idList = new ArrayList<>();
        List<Answers> answerList = null;
        for (Questions q : questList) {
            idList.add(q.getId());
            answerList = answersDao.getMaxUpCountAnswersForView(q.getId(), null, 3);
            q.setAnswers(answerList);
        }
//        if (proposeUserId != null) {
//            List<Vote> voteList = voteDao.getVoteList(FlyContentType.QUESTION.value(), proposeUserId, idList);
//            for (Vote v : voteList) {
//                int index = idList.indexOf(v.getContentId());
//                questList.get(index).setIVote(v.getVoteType());
//            }
//        }
        return questList;
    }

    public int getQuestionCountForCustom(String title, Integer status) throws Exception {
        return questionsDao.getQuestionCountForCustom(title, StatusConst.OPEN.value());
    }
    
    public List<Questions> getQuestionListHot(int pageIndex, int pageSize)
            throws Exception {
        List<Questions> questList = questionsDao.getHotQuestions(StatusConst.OPEN.value(), 0, 5);
        //
        List<Answers> answerList = null;
        for (Questions q : questList) {
            answerList = answersDao.getMaxUpCountAnswersForView(q.getId(), null, 1);
            q.setAnswers(answerList);
        }
        // 用户访问只看到开启中的问题列表
        
//        for (Questions q : questList) {
//            int index = idList.indexOf(q.getId());
//            q.setAnswers(answerList.get(index));
//        }
        return questList;
    }
    
    public Questions queryQuestionById(Long id) throws Exception {
        Questions quest = questionsDao.queryQuestionById(id, null);
        if (quest != null) {
//            Vote v = voteDao.queryVote(FlyContentType.QUESTION.value(), quest.getProposeUserId(), id);
//            if (v != null) {
//                quest.setIVote(v.getVoteType());
//            }
        }
        return quest;
    }
    
    public Questions queryQuestionById(Long id, Long viewUserId) throws Exception {
        Questions quest = questionsDao.queryQuestionById(id, null);
        //非发表人查看，管理员不用这个
        if (quest != null && !Objects.equals(viewUserId, quest.getProposeUserId())) {
            questionCountDao.updateCountIncrease(id, 1);
        }
        return quest;
    }
    
    @Transactional
    public int auditQuestion(Long id, Integer status, String opinion, Date updateTime, Long updateUserId)
            throws Exception {
        Questions question = new Questions();
        question.setId(id);
        question.setStatus(status);
        question.setOpinion(opinion);
        question.setUpdateTime(updateTime);
        question.setUpdateUserId(updateUserId);
        return questionsDao.editQuestion(question);
    }
    
    @Transactional
    public int editQuestions(Long id, Integer status, Date updateTime, Long updateUserId) throws Exception {
        Questions question = new Questions();
        question.setId(id);
        question.setStatus(status);
        question.setUpdateTime(updateTime);
        question.setUpdateUserId(updateUserId);
        questionsDao.editQuestion(question);
        answersDao.editAnswerByQuestionId(id, status, updateTime, updateUserId);
        return 1;
    }
    
    @Transactional
    public int removeQuestion(Long id) throws Exception {
        questionsDao.removeQuestionById(id);
        return answersDao.removeAnswerByQuestionId(id);
    }
    
}
